<template>
  <view class="search-container">
    <view class="search-box">
      <uni-search-bar
        :radius="100"
        placeholder="请输入搜索内容"
        @input="input"
        cancelButton="none"
        @clear="goodsList = []"
      ></uni-search-bar>
    </view>
    <view v-if="goodsList.length === 0" class="history-box">
      <view class="history-title">
        <text>搜索历史</text>
        <uni-icons @click="clear" type="trash" size="17" />
      </view>
      <view class="history-list">
        <uni-tag
          @click="goToGoodsList(item)"
          v-for="item in historyList"
          :key="item"
          :text="item"
        ></uni-tag>
      </view>
    </view>
    <view v-else class="sugg-list">
      <view
        class="sugg-item"
        v-for="item in goodsList"
        :key="item.goods_id"
        @click="goToDetail(item.goods_id)"
      >
        <text class="goods-name">{{ item.goods_name }}</text>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  data () {
    return {
      tiemr: null,
      goodsList: [], // 查询到商品列表
      historyList: [] // 搜索历史
    }
  },
  onLoad () {
    this.historyList = uni.getStorageSync('history') || []
  },
  methods: {
    input (val) {
      clearTimeout(this.timer)

      this.timer = setTimeout(() => {
        if (val.trim().length !== 0) {
          this.getGoodsListData(val)
        }
      }, 500)
    },
    async getGoodsListData (keyword) {
      // 记录搜索关键词
      this.saveKeyword(keyword)

      const res = await uni.$request({
        url: 'goods/qsearch',
        data: {
          query: keyword
        }
      })

      if (res.meta.status === 200) {
        this.goodsList = res.message
      }
    },
    // 保存搜索关键词
    saveKeyword (keyword) {
      // 干掉已有的关键字
      const findIndex = this.historyList.findIndex(item => item === keyword)
      if (findIndex > -1) {
        this.historyList.splice(findIndex, 1)
      }

      this.historyList.unshift(keyword)

      uni.setStorageSync('history', this.historyList)
    },
    goToDetail (goods_id) {
      uni.navigateTo({ url: '/subpkg/goods-detail/index?id=' + goods_id })
    },
    clear () {
      this.historyList = []
      uni.setStorageSync('history', [])
    },
    goToGoodsList (query) {
      uni.navigateTo({ url: '/subpkg/goods-list/index?query=' + query })
    }
  }
}
</script>

<style lang="scss" scoped>
.search-container {
  .search-box {
    position: sticky;
    top: 0;
    z-index: 999;
    ::v-deep .uni-searchbar {
      /* #ifndef APP-NVUE */
      display: flex;
      /* #endif */
      flex-direction: row;
      position: relative;
      padding: 16rpx;
      /* 将默认的 #FFFFFF 改为 #C00000 */
      background-color: #c00000;
    }
  }

  .history-box {
    padding: 0 5px;

    .history-title {
      display: flex;
      justify-content: space-between;
      align-items: center;
      height: 40px;
      font-size: 13px;
      border-bottom: 1px solid #efefef;
    }

    .history-list {
      display: flex;
      flex-wrap: wrap;

      ::v-deep .uni-tag {
        margin-top: 5px;
        margin-right: 5px;
      }
    }
  }

  .sugg-list {
    padding: 0 5px;

    .sugg-item {
      font-size: 12px;
      padding: 13px 0;
      border-bottom: 1px solid #efefef;
      display: flex;
      align-items: center;
      justify-content: space-between;

      .goods-name {
        // 文字不允许换行（单行文本）
        white-space: nowrap;
        // 溢出部分隐藏
        overflow: hidden;
        // 文本溢出后，使用 ... 代替
        text-overflow: ellipsis;
        margin-right: 3px;
      }
    }
  }
}
</style>
